package com.langll.mcp;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.apache.ibatis.jdbc.ScriptRunner;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;

public class CodeGenerator {

    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://127.0.0.1:3306/ppm?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "rootpassword");

    private static final String PROJECT_BASEPATH = "/home/myworkspace/lang-rag/ai-springboot-mcp-server";

    private static final String TABLENAME = "portfolio_product";
    private static final String MODULENAME = "portfolio";



    /**
     * 执行 run
     */
    public static void main(String[] args) throws SQLException {

        // 配置模板
        String absolutePath = File.separator + "codeGeneratorTemplates";
        String serviceTempPath = absolutePath + File.separator + File.separator + "ServiceP";
        String serviceImplTempPath = absolutePath + File.separator + File.separator + "ServiceImplP";
        String controllerTempPath = absolutePath + File.separator + "Controller";

        //代码生成器
        //需要高级定制请前往
        // https://baomidou.com/pages/981406/#%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE-datasourceconfig

        // 初始化数据库脚本
        //  initDataSource(DATA_SOURCE_CONFIG.build());
        //自动生成数据
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig(builder -> {
                    builder.author("langll2") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .dateType(DateType.TIME_PACK) // 时间策略
                            .commentDate("yyyy-MM-dd") //注释日期格式
                            .outputDir(PROJECT_BASEPATH + "/src/main/java"); // 指定输出目录，根据自己的路径设置到main\java下
                })
                .packageConfig(builder -> {
                    builder.parent("com.langll.mcp") // 设置父包名
                            .moduleName(MODULENAME) // 设置父包模块名
                            .entity("model") //实体类目录
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
//                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, PROJECT_BASEPATH + "/src/main/resources/mapper")); // 设置mapperXml生成路径,集中放置,减少xml的sql的使用
                }).templateConfig(templateConfig-> templateConfig
                        .service(serviceTempPath)
                        .serviceImpl(serviceImplTempPath))
//                        .controller(controllerTempPath))
                .strategyConfig(builder -> {
                    builder.addInclude(TABLENAME) // 设置需要生成的表名
                            .entityBuilder()
                            .addTableFills(new Column("create_time", FieldFill.INSERT))
                            .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                            .addTableFills(new Column("creator", FieldFill.INSERT))
                            .addTableFills(new Column("modifier", FieldFill.INSERT_UPDATE))
                            .mapperBuilder()
                            .enableMapperAnnotation();
                })
                .execute();
    }

    /**
     * 执行数据库脚本
     */
    public static void initDataSource(DataSourceConfig dataSourceConfig) throws SQLException {
        Connection conn = dataSourceConfig.getConn();
        InputStream inputStream = CodeGenerator.class.getResourceAsStream("/db/sql/init.sql");
        ScriptRunner scriptRunner = new ScriptRunner(conn);
        scriptRunner.setAutoCommit(true);
        scriptRunner.runScript(new InputStreamReader(inputStream));
        conn.close();
    }

}